<script setup>
import { onMounted, reactive, ref, toRefs } from 'vue';
import { Search } from '@element-plus/icons-vue';
import Pagination from '@/components/Pagination/index.vue';

import { queryHistoryPcbaPcbOrderList } from '@/server/api/biz.js';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
});

const emits = defineEmits(['update:modelValue', 'close', 'selected']);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serial: '',
  },
});
const currentRow = ref(null);
const list = ref([]); // 已下单订单列表
const total = ref(0);
const { queryParams } = toRefs(data);
const radio = ref();

onMounted(() => {
  getList();
});

// 关闭弹窗
const onClose = () => {
  emits('update:modelValue', false);
};

// 查询
const handleQuery = () => {
  getList();
};

let fileId = ref();

// 选中列
const onSelectedRow = (row) => {
  currentRow.value = row;
  fileId.value = row?.fileVO.id;
};

/**
 * 获取已下单订单列表
 */
const getList = async () => {
  const res = await queryHistoryPcbaPcbOrderList({
    ...data.queryParams,
  });
  if (res.code === 200) {
    list.value = res.data.list;
    total.value = res.data.totalRow;
  } else {
    ElMessage.error(res.msg);
  }
};

/**
 * 确认选择
 */
const handleSubmit = () => {
  if (!currentRow.value) {
    return ElMessage.warning(t('mall.select_an_order'));
  }
  emits('close');
  emits('selected', currentRow.value);
};

// 格式化订单状态
const formatOrderStatus = (status) => {
  const orderStatusMap = {
    1: t('mall.matching'),
    2: t('mall.pending_payment'),
    3: t('mall.production_confirmation_pending'),
    4: t('mall.in_production'),
    5: t('mall.awaiting_shipment'),
    6: t('mall.awaiting_receipt'),
    7: t('mall.received'),
    8: t('mall.completed'),
    9: t('mall.refunding'),
    10: t('mall.refunded'),
    11: t('mall.cancelled'),
    12: t('mall.deleted'),
  };
  return orderStatusMap[status] || '--';
};
</script>

<template>
  <div class="select-modal">
    <!-- 创建渠道弹窗 -->
    <el-dialog
      v-model="props.modelValue"
      :title="$t('smt.selectTheOrder')"
      center
      :close-on-click-modal="false"
      :before-close="onClose"
    >
      <el-row type="flex" justify="end">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
          <el-form-item prop="serial" style="margin-bottom: 12px">
            <el-input
              v-model="queryParams.serial"
              :placeholder="$t('smt.searchForYourOrderNumber')"
              clearable
              style="width: 254px"
              @keyup.enter="handleQuery"
            >
              <template #suffix>
                <el-icon @click="handleQuery" class="icon"><Search /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table :data="list" @current-change="onSelectedRow" border style="width: 100%">
        <el-table-column width="50px" align="center">
          <template #default="scope">
            <el-radio v-model="radio" :label="scope.row.id">
              <span></span>
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="serial" :label="$t('smt.orderNo')" width="180" />
        <el-table-column prop="createTime" :label="$t('smt.Ordertime')" width="180" />
        <el-table-column prop="fileName" :label="$t('smt.PCBFilename')">
          <template #default="scope">
            <div>
              {{ scope.row.fileVO.fileName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="productName" :label="$t('smt.productCategory')" />
        <el-table-column prop="orderStatus" :label="$t('smt.orderStatus')">
          <template #default="scope">
            <div>
              {{ formatOrderStatus(scope.row.orderStatus) }}
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <template #footer>
        <span class="dialog-footer">
          <el-button palin @click="onClose">{{ $t('smt.Cancel') }}</el-button>
          <el-button type="primary" @click="handleSubmit()"> {{ $t('smt.Define') }} </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.icon {
  cursor: pointer;
}
</style>
